package gui2.g2;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class EDTCorrec extends JFrame {

	private JProgressBar bar;
	private JButton load;

	public EDTCorrec() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		bar = new JProgressBar(0, 100);
		bar.setPreferredSize(new Dimension(400, 50));

		load = new JButton("Load!");
		load.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				load.setEnabled(false);
				bar.setString("Loading...");
				bar.setStringPainted(true);

				new Thread() {
					public void run() {

						for (int i = 0; i <= 100; i++) {
							final int iFinal = i;
							EventQueue.invokeLater(new Runnable() {
								public void run() {
									bar.setValue(bar.getValue() + 1);
									bar.setString("Loading... " + iFinal + "%");
								};
							});

							try {
								Thread.sleep((long) (Math.random() * 150));
							} catch (InterruptedException e1) {
								e1.printStackTrace();
							}

						}

						EventQueue.invokeLater(new Runnable() {
							public void run() {
								bar.setValue(0);
								bar.setStringPainted(false);
								load.setEnabled(true);
							};
						});

					}
				}.start();

			}
		});

		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(load, BorderLayout.NORTH);
		getContentPane().add(bar, BorderLayout.CENTER);
		pack();
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				JFrame f = new EDTCorrec();
				f.setLocationRelativeTo(null);
				f.setVisible(true);
			}
		});
	}
}
